import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:photo_frame/net/model/asset_trans_model.dart';
import 'package:photo_frame/net/model/asset_trans_record_model.dart';
import 'package:photo_frame/widgets/app_asset_image_widget.dart';
import 'package:photo_manager/photo_manager.dart';

class SendAssetRecordWidget extends StatelessWidget {
  final AssetTransRecordModel recordModel;
  final void Function(AssetTransRecordModel) resend;
  final void Function(AssetTransRecordModel) delete;

  SendAssetRecordWidget(
      {required this.recordModel,
      required this.resend,
      required this.delete,
      super.key});

  @override
  Widget build(BuildContext context) {
    AssetTransModel transModel = recordModel.transAssetList.first;
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          color: Colors.white,
          padding: EdgeInsets.only(left: 10, right: 15, top: 10, bottom: 10),
          child: IntrinsicHeight(
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                SendAssetRecordPicWallWidget(
                    assetIds: recordModel.transAssetList
                        .map((t) => "${t.assetId}")
                        .toList()),
                SizedBox(
                  width: 10,
                ),
                Expanded(
                    child: Column(
                  children: [
                    SizedBox(height: 8),
                    Row(children: [
                      Expanded(
                          child: Text(
                        style: TextStyle(fontWeight: FontWeight.bold),
                        "${transModel.toFrameName}",
                        textAlign: TextAlign.left,
                      )),
                      Text("")
                    ]),
                    Expanded(child: SizedBox()),
                    Row(children: [
                      Expanded(
                        child: Text(
                          DateFormat('yyyy/MM/dd').format(
                            DateTime.fromMillisecondsSinceEpoch(
                              int.parse(recordModel.transDate ??
                                  DateTime.timestamp()
                                      .millisecondsSinceEpoch
                                      .toString()),
                            ),
                          ),
                        ),
                      ),
                      CupertinoButton(
                          padding: EdgeInsets.zero,
                          onPressed: () {
                            resend(recordModel);
                          },
                          child: SizedBox(
                            width: 40,
                            height: 40,
                            child: Align(
                                alignment: Alignment.centerRight,
                                child: Image.asset(
                                    width: 20,
                                    height: 20,
                                    "images/icon_asset_record_trans.png")),
                          )),
                      CupertinoButton(
                          padding: EdgeInsets.zero,
                          onPressed: () {
                            delete(recordModel);
                          },
                          child: SizedBox(
                            width: 40,
                            height: 40,
                            child: Align(
                                alignment: Alignment.centerRight,
                                child: Image.asset(
                                    width: 20,
                                    height: 20,
                                    "images/icon_asset_record_delete.png")),
                          )),
                    ])
                  ],
                ))
              ],
            ),
          ),
        ),
        Container(
          color: Color(0xfff2f2f2),
          height: 10,
        )
      ],
    );
  }
}

class SendAssetRecordPicWallWidget extends StatelessWidget {
  final List<String> assetIds;
  SendAssetRecordPicWallWidget({required this.assetIds, super.key});
  @override
  Widget build(BuildContext context) {
    if (assetIds.length <= 1) {
      return buildFutureImage(assetIds.first, 90, 90);
    } else if (assetIds.length == 2) {
      return SizedBox(
          width: 90,
          height: 90,
          child: Row(children: [
            buildFutureImage(assetIds.first, 44, 90),
            SizedBox(width: 2),
            buildFutureImage(assetIds.last, 44, 90)
          ]));
    } else if (assetIds.length == 3) {
      return SizedBox(
        width: 90,
        height: 90,
        child: Row(
          children: [
            buildFutureImage(assetIds.first, 44, 90),
            SizedBox(width: 2),
            Column(
              children: [
                buildFutureImage(assetIds[1], 44, 44),
                SizedBox(height: 2),
                buildFutureImage(assetIds.first, 44, 44)
              ],
            )
          ],
        ),
      );
    } else {
      return SizedBox(
        width: 90,
        height: 90,
        child: Column(
          children: [
            Row(
              children: [
                buildFutureImage(assetIds[0], 44, 44),
                SizedBox(width: 2),
                buildFutureImage(assetIds[1], 44, 44),
              ],
            ),
            SizedBox(height: 2),
            Row(
              children: [
                buildFutureImage(assetIds[2], 44, 44),
                SizedBox(width: 2),
                SizedBox(
                  width: 44,
                  height: 44,
                  child: Stack(
                    children: [
                      buildFutureImage(assetIds[3], 44, 44),
                      assetIds.length > 4
                          ? Container(
                              color: Color(0x33000000),
                              child: Center(
                                child: Text(
                                  "+${assetIds.length}",
                                  style: TextStyle(
                                      color: Colors.white,
                                      fontWeight: FontWeight.bold,
                                      fontSize: 14),
                                ),
                              ),
                            )
                          : SizedBox()
                    ],
                  ),
                )
              ],
            )
          ],
        ),
      );
    }
  }
}

Future<AssetEntity?> fetchAsset(String assetId) async {
  AssetEntity? e = await AssetEntity.fromId(assetId);
  return e;
}

Widget buildFutureImage(String assetId, double width, double height) {
  return SizedBox(
      width: width,
      height: height,
      child: FutureBuilder<AssetEntity?>(
          future: fetchAsset(assetId),
          builder:
              (BuildContext context, AsyncSnapshot<AssetEntity?> snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting ||
                snapshot.hasError ||
                snapshot.data == null) {
              return Container(
                width: width,
                height: height,
                color: Color(0xffF2F2F2),
              );
            } else {
              return AppAssetImageWidget(
                onPreview: false,
                selected: false,
                entity: snapshot.data!,
                option: const ThumbnailOption(size: ThumbnailSize.square(200)),
              );
            }
          }));
}
